<template>
<div id="app">
    <div class="nav-bar">
        <img src="./assets/logo1.png" class="header-logo">
        <div id="nav">
            <router-link to="/">主页</router-link>
            <router-link to="/site">景点信息</router-link>
            <router-link to="/hotel">民宿信息</router-link>
			<router-link to="/product">美食信息</router-link>
			<router-link to="/activity">人文活动</router-link>
            <router-link to="/line">旅游路线</router-link>
            <router-link to="/ich">非遗文化</router-link>
            <router-link to="/announcement">通知公告</router-link>
			<router-link to="/feedback">游客反馈</router-link>
<!--            <router-link to="/about">关于我们</router-link>-->
        </div>
        <div class="header-user">
            <el-popover placement="top-start" title="" width="80" trigger="hover" content="">
            <div class="personal-wrapper" v-if="userName != ''">
                <div class="personal-item" @click="goToPersonal()">个人主页</div>
                <div class="personal-item" @click="logout()">退出登录</div>
            </div>
            <div class="personal-wrapper" v-else>
                <div class="personal-item" @click="goToLogin()">登录</div>
            </div>
                <a @click="handleUserClick()" slot="reference">
                    <img v-if="userProfile && userProfile !== ''" class="login-icon" :src="userProfile" />
                    <el-avatar v-else class="login-icon" :icon="userName ? 'el-icon-user-solid' : 'el-icon-user'">
                        <span v-if="userType === 'admin'" class="admin-text">管理员</span>
                    </el-avatar>
                </a>
            </el-popover>

        </div>
    </div>
    <router-view style="padding-top: 60px" />
</div>
</template>

<style lang="scss">
.nav-bar {
    position: fixed;
    z-index: 10;
    width: 100%;
}

el-menu-item {
    height: 30px;
}

.personal-wrapper {
    text-align: center;
}

.personal-item {
    cursor: pointer;
    line-height: 40px;
}

.personal-item :hover {
    background-color: #aaaaaa;
}

.header-logo {
    height: 50px;
    position: fixed;
    left: 10px;
    top: 5px;
    z-index: 10;
}

.login-icon {
    z-index: 1;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: solid 2px lightgray;
    cursor: pointer;
}

.admin-text {
    font-size: 10px;
    color: #409EFF;
    font-weight: bold;
}

.header-user {
    position: fixed;
    right: 30px;
    top: 10px;
    height: 50px;
    line-height: 50px;
}

#app {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
}

#nav {
    border-bottom: solid 0.5px #42b983;
    background-color: white;
    padding: 20px;
    position: fixed;
    width: 100%;
    top: 0px;
    text-align: center;

    a {
        font-weight: normal;
        color: #2c3e50;
        text-decoration: none;
        margin-left: 80px;

        &.router-link-exact-active {
            color: #42b983;
        }
    }
}
</style>

<script>
export default {
    name: 'vue_main',
    mounted() {
        this.updateUserState();
        // 监听localStorage变化
        window.addEventListener('storage', this.handleStorageChange);
        // 监听自定义事件
        window.addEventListener('userStateChanged', this.updateUserState);
    },
    beforeDestroy() {
        // 清理事件监听
        window.removeEventListener('storage', this.handleStorageChange);
        window.removeEventListener('userStateChanged', this.updateUserState);
    },
    data() {
        return {
            userName: '',
            userType: '',
            userProfile: ''
        }
    },
    methods: {
        // 更新用户状态
        updateUserState() {
            this.userName = window.localStorage.getItem('userName') || '';
            this.userType = window.localStorage.getItem('userType') || '';
            this.userProfile = window.localStorage.getItem('userProfile') || '';
        },
        
        // 处理localStorage变化
        handleStorageChange(e) {
            if (e.key === 'userName' || e.key === 'userType' || e.key === 'userProfile') {
                this.updateUserState();
            }
        },
        handleUserClick() {
            // 点击头像的处理逻辑
            if (this.userName) {
                // 已登录用户，不需要额外处理，popover会自动显示菜单
                return;
            } else {
                // 未登录用户，跳转到登录页面
                this.goToLogin();
            }
        },
        goToLogin() {
            this.$router.push('/login');
        },
        goToPersonal() {
            if (this.userType === 'admin') {
                this.$router.push('/manage');
            } else if (this.userType === 'user') {
                this.$router.push('/personal');
            }
        },
        logout() {
            window.localStorage.setItem('userName', '');
            window.localStorage.setItem('userType', '');
            window.localStorage.setItem('userProfile', '');
            this.userName = '';
            this.userType = '';
            this.userProfile = '';
            // 触发自定义事件通知其他组件更新状态
            window.dispatchEvent(new CustomEvent('userStateChanged'));
            this.$router.push('/');
            this.$message.success("退出登录成功");
        }
    }
};
</script>
